package top.tupobi.android_framework_test

import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import top.tupobi.android_framework.TPermission

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        btn_call.setOnClickListener {
            if (TPermission.checkSinglePermission(this, Manifest.permission.CALL_PHONE)) {
                Log.e("TAG", "permission request check passed")
                call()
            } else {
                TPermission.requestPermissions(
                    this,
                    Manifest.permission.CALL_PHONE
                ) { allGranted, deniedList ->
                    if (allGranted) {
                        Log.e("TAG", "permission granted")
                        call()
                    } else {
                        Toast.makeText(this, "deny == $deniedList", Toast.LENGTH_LONG).show()
                    }
                }
            }

        }
    }

    private fun call() {
        try {
            Log.e("TAG", "call")
            val intent = Intent(Intent.ACTION_CALL)
            intent.data = Uri.parse("tel:10086")
            startActivity(intent)
        } catch (e: SecurityException) {
            e.printStackTrace()
        }

    }
}